/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.controllers.CampaignMasterController;
import com.insurance.cis.daos.CampaignDao;
import com.insurance.cis.models.Campaign;
import com.insurance.cis.scos.CampaignSearchCondition;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TruongLQ
 */
public class EstimatedCampaignJPanelView extends javax.swing.JPanel {

    DefaultTableModel dtm;
    CampaignMasterController controller = new CampaignMasterController(this);
    final MainJFrameView mainJFrame;
    final CampaignDao campaignDao = new CampaignDao();

    /**
     * Creates new form EstimatedCampaignJPanelView
     */
    public EstimatedCampaignJPanelView(javax.swing.JFrame jFrame) {
        initComponents();
        loadTableMetaData();
        mainJFrame = (MainJFrameView) jFrame;
    }

    private void loadTableMetaData() {
        Vector colHeader = new Vector(12);
        colHeader.add("Campaign ID");
        colHeader.add("Campaign Name");
        colHeader.add("Product ID");
        colHeader.add("Product Name");
        colHeader.add("Product Category");
        colHeader.add("Channel Desc");
        colHeader.add("Discount (%)");
        colHeader.add("Gift");
        colHeader.add("Estimated sales");
        colHeader.add("Estimated budget");
        colHeader.add("Start Date");
        colHeader.add("End Date");
        dtm = new DefaultTableModel(colHeader, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblCampaign.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnUpdate = new javax.swing.JButton();
        bntReset = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtCampaignID = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtCampaignName = new javax.swing.JTextField();
        txtStartDate = new javax.swing.JTextField();
        txtChannelID = new javax.swing.JTextField();
        txtProductID = new javax.swing.JTextField();
        txtEndDate = new javax.swing.JTextField();
        btnSelectChannel = new javax.swing.JButton();
        btnSelectProduct = new javax.swing.JButton();
        btnSearch = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCampaign = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtEstimatedSales = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtEstimatedBudget = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtSelectedCampaignID = new javax.swing.JTextField();
        txtSelectedCampaignName = new javax.swing.JTextField();

        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        bntReset.setText("Reset");
        bntReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntResetActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Search criteria", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel6.setText("End date");

        jLabel5.setText("Product ID");

        jLabel7.setText("Start date");

        jLabel8.setText("Campaign Name");

        jLabel9.setText("Channel ID");

        jLabel10.setText("Campaign ID");

        btnSelectChannel.setText("Select");
        btnSelectChannel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectChannelActionPerformed(evt);
            }
        });

        btnSelectProduct.setText("Select");
        btnSelectProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectProductActionPerformed(evt);
            }
        });

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCampaignID, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                            .addComponent(txtCampaignName)
                            .addComponent(txtStartDate))
                        .addGap(31, 31, 31)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtChannelID, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                                    .addComponent(txtProductID))
                                .addGap(22, 22, 22)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnSelectProduct, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnSelectChannel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addComponent(txtEndDate))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCampaignID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel9)
                    .addComponent(txtChannelID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSelectChannel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCampaignName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel5)
                    .addComponent(txtProductID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSelectProduct))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch)
                    .addComponent(btnReset))
                .addContainerGap())
        );

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel11.setText("Estimate sales and budget for a campaign");

        tblCampaign.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblCampaign.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblCampaignMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblCampaign);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Selected campaign", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel2.setText("Estimated sales");

        jLabel1.setText("Estimated budget");

        jLabel12.setText("Campaign ID");

        jLabel13.setText("Campaign Name");

        txtSelectedCampaignID.setEditable(false);

        txtSelectedCampaignName.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(18, 18, 18)
                        .addComponent(txtSelectedCampaignID, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(18, 18, 18)
                        .addComponent(txtSelectedCampaignName, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtEstimatedBudget, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtEstimatedSales, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(173, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel12, jLabel13});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtSelectedCampaignID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtEstimatedSales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtSelectedCampaignName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtEstimatedBudget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnUpdate)
                        .addGap(18, 18, 18)
                        .addComponent(bntReset)
                        .addGap(18, 18, 18)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel11)
                            .addComponent(jScrollPane2)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(67, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bntReset, btnClose, btnUpdate});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(bntReset)
                    .addComponent(btnUpdate))
                .addContainerGap(28, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bntResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntResetActionPerformed
        String campaignID = txtSelectedCampaignID.getText();
        if (campaignID.isEmpty()) {
            resetAll();
        } else {
            Campaign campaign = campaignDao.getByID(campaignID);
            loadSelectedCampaign(campaign);
        }
    }//GEN-LAST:event_bntResetActionPerformed

    private void resetAll() {
        txtSelectedCampaignID.setText("");
        txtSelectedCampaignName.setText("");
        txtEstimatedBudget.setText("");
        txtEstimatedSales.setText("");
    }
    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        mainJFrame.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        updateCampaign();

    }//GEN-LAST:event_btnUpdateActionPerformed

    public void clearAllFields() {
        resetAll();
        dtm.getDataVector().clear();
        btnResetActionPerformed(null);
    }
    private void btnSelectChannelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectChannelActionPerformed
        final EstimatedCampaignJPanelView estimatedJpnl = this;
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ChannelJDialogChooser dialog = new ChannelJDialogChooser(
                        mainJFrame, true, estimatedJpnl);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
            }
        });
    }//GEN-LAST:event_btnSelectChannelActionPerformed

    private void btnSelectProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectProductActionPerformed
        final EstimatedCampaignJPanelView estimatedJpnl = this;
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ProductJDialogChooser dialog = new ProductJDialogChooser(
                        mainJFrame, true, estimatedJpnl);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
            }
        });
    }//GEN-LAST:event_btnSelectProductActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        CampaignSearchCondition sco = controller.validateSearchConditions();
        if (sco == null) {
            return;
        }
        Date today = new Date();
        String todayStr = Utils.convertDateToString(today, CisConstants.DATE_DD_MM_YYYY);
        try {
            // limit the search result
            if (sco.getStartDate() == null || sco.getStartDate().before(today)) {
                sco.setStartDate(Utils.parseDate(todayStr, CisConstants.DATE_DD_MM_YYYY));
            }
        } catch (ParseException ex) {
            sco.setStartDate(null);
        }
        List<Campaign> campaigns = campaignDao.searchCampaigns(sco);
        if (!campaigns.isEmpty()) {
            loadResults(campaigns);
        } else {
            MessageDialogUtils.dialogMessage(this, "No campaigns found");
            dtm.getDataVector().clear();
            tblCampaign.updateUI();
        }
        resetAll();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void loadResults(List<Campaign> campaigns) {
        dtm.getDataVector().clear();
        for (Campaign campaign : campaigns) {
            dtm.addRow(new Object[]{
                        campaign.getCampaignID(),
                        campaign.getCampaignName(),
                        campaign.getProduct().getId(),
                        campaign.getProduct().getName(),
                        campaign.getProduct().getCategory().getDesc(),
                        campaign.getChannel().getChannelDesc(),
                        campaign.getPromotion().getDiscount(),
                        campaign.getPromotion().getGift(),
                        BigDecimal.valueOf(campaign.getEstimatedSale()).toPlainString(),
                        BigDecimal.valueOf(campaign.getEstimatedBudget()).toPlainString(),
                        campaign.getStartDateStr(),
                        campaign.getEndDateStr()
                    });
        }
    }

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        txtCampaignID.setText("");
        txtCampaignName.setText("");
        txtChannelID.setText("");
        txtEndDate.setText("");
        txtProductID.setText("");
        txtStartDate.setText("");
    }//GEN-LAST:event_btnResetActionPerformed

    private void tblCampaignMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblCampaignMouseClicked
        int row = tblCampaign.getSelectedRow();
        if (row < 0 || row >= tblCampaign.getRowCount()) {
            return;
        }

        Campaign chosenCampaign = campaignDao.getByID((String) dtm.getValueAt(row, 0));
        loadSelectedCampaign(chosenCampaign);
    }//GEN-LAST:event_tblCampaignMouseClicked

    private void loadSelectedCampaign(Campaign chosenCampaign) {
        txtSelectedCampaignID.setText(chosenCampaign.getCampaignID());
        txtSelectedCampaignName.setText(chosenCampaign.getCampaignName());
        txtEstimatedBudget.setText(BigDecimal.valueOf(chosenCampaign.getEstimatedBudget()).toPlainString());
        txtEstimatedSales.setText(BigDecimal.valueOf(chosenCampaign.getEstimatedSale()).toPlainString());
    }

    private boolean validateEstimatedInput() {
        String estimatedSalesValue = txtEstimatedSales.getText().trim();
        String estimatedBudgetValue = txtEstimatedBudget.getText().trim();
        if (!estimatedSalesValue.isEmpty() && !estimatedSalesValue.matches("^[0-9]+(\\.[0-9]+)*$")) {
            MessageDialogUtils.dialogError(this, "Estimated sales must be numeric!");
            txtEstimatedSales.requestFocus();
            return false;
        } else if (!estimatedBudgetValue.isEmpty() && !estimatedBudgetValue.matches("^[0-9]+(\\.[0-9]+)*$")) {
            MessageDialogUtils.dialogError(this, "Estimated budget must be numeric!");
            txtEstimatedBudget.requestFocus();
            return false;
        }

        if (!estimatedSalesValue.isEmpty()) {
            double estimatedSalesValueDouble = Double.valueOf(estimatedSalesValue);
            if (estimatedSalesValueDouble >= Double.valueOf(1000000000)) { // maximum: 1.000.000.000
                MessageDialogUtils.dialogError(this, "Estimated sales must be lower than 1 billion dollar!");
                txtEstimatedSales.requestFocus();
                return false;
            }
        }
        
        if (!estimatedBudgetValue.isEmpty()) {
            double estimatedBudgetValueDouble = Double.valueOf(estimatedBudgetValue);
            if (estimatedBudgetValueDouble >= Double.valueOf(1000000000)) { // maximum: 1.000.000.000
                MessageDialogUtils.dialogError(this, "Estimated budget must be lower than 1 billion dollar!");
                txtEstimatedBudget.requestFocus();
                return false;
            }
        }

        return true;
    }

    private void updateCampaign() {
        if (txtSelectedCampaignID.getText().isEmpty()) {
            MessageDialogUtils.dialogError(this, "You must choose one item to update!");
            return;
        }
        if (!validateEstimatedInput()) {
            return;
        }
        String campaignID = txtSelectedCampaignID.getText();
        String estimatedSalesValue = txtEstimatedSales.getText().trim();
        String estimatedBudgetValue = txtEstimatedBudget.getText().trim();
        double estimatedBudget = estimatedBudgetValue.isEmpty() ? 0 : Double.valueOf(estimatedBudgetValue);
        double estimatedSales = estimatedSalesValue.isEmpty() ? 0 : Double.valueOf(estimatedSalesValue);

        campaignDao.updateEstimatedData(campaignID, estimatedSales, estimatedBudget);
        MessageDialogUtils.dialogMessage(mainJFrame, "Update estimated data succesful");
        btnSearchActionPerformed(null);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntReset;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSelectChannel;
    private javax.swing.JButton btnSelectProduct;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblCampaign;
    private javax.swing.JTextField txtCampaignID;
    private javax.swing.JTextField txtCampaignName;
    private javax.swing.JTextField txtChannelID;
    private javax.swing.JTextField txtEndDate;
    private javax.swing.JTextField txtEstimatedBudget;
    private javax.swing.JTextField txtEstimatedSales;
    private javax.swing.JTextField txtProductID;
    private javax.swing.JTextField txtSelectedCampaignID;
    private javax.swing.JTextField txtSelectedCampaignName;
    private javax.swing.JTextField txtStartDate;
    // End of variables declaration//GEN-END:variables

    public JTextField getTxtCampaignID() {
        return txtCampaignID;
    }

    public JTextField getTxtCampaignName() {
        return txtCampaignName;
    }

    public JTextField getTxtChannelID() {
        return txtChannelID;
    }

    public JTextField getTxtEndDate() {
        return txtEndDate;
    }

    public JTextField getTxtEstimatedBudget() {
        return txtEstimatedBudget;
    }

    public JTextField getTxtEstimatedSales() {
        return txtEstimatedSales;
    }

    public JTextField getTxtProductID() {
        return txtProductID;
    }

    public JTextField getTxtStartDate() {
        return txtStartDate;
    }
}
